<template>
	<view>
		<image src="https://img.zzplc.net/stc/beijing.jpg" class="img" mode="widthFix"></image>
		<block v-if="flag==1||mobile!=''">
			<view class="button" @click="index">立即查看</view>
		</block>
		<block v-else>
			<view class="button" @click="clickButton">立即查看</view>
		</block>
		
		<!-- <view class="button" @click="clickButton">立即查看</view> -->
		
		<!-- <block v-if="!mobile">
			<button class="button" open-type="getPhoneNumber" @getphonenumber="getphonenumber">立即查看</button>
		</block> -->
		
		<updataUser :login="true" :indexs="showMobile" :userInfo="userInfo" @success="updataSuccess" @successPhone="successPhone" @click="hidePopup" v-if="showUserInfo"></updataUser>
		
		<!-- <updataUser :login="true" :index="2" :userInfo="userInfo" @success="updataSuccess" @successPhone="successPhone" @click="hidePopup" v-if="showUserInfo"></updataUser> -->
		
		<!-- 弹窗 -->
		<block v-if="popup==1">
			<view class="popup"></view>
			<image class="popup_img" src="/static/popup_img.png"></image>
			<view class="popup_button" @click="clickPopup">更换授权手机号</view>
			<image class="popup_close" src="/static/popup_close.png" @click="close"></image>
		</block>
		
	</view>
</template>

<script>
	import updataUser from 'components/updataUser'
	export default {
		components: {
			updataUser
		},
		data() {
			return {
				userinfo: '',
				mobile: '',
				flag: 0,
				code: '',
				showPhone: false,
				userInfo: '',
				showUserInfo: false,
				nickname: '',
				popup: false,
				showMobile: 0,
			};
		},
		
		onLoad(options) {
			let that = this
			
			that.popup = options.popup
			
			// that.mobile = uni.getStorageSync('mobile')
			uni.login({
				success(login) {
					wx.request({
						url: getApp().globalData.url + '/sys/reg',
						method: 'POST',
						data: {
							code: login.code
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded',
						},
						success: (res) => {
							if (res.data.code == 20000) {
								that.userinfo = res.data.data
								that.mobile = res.data.data.mobile
								that.flag = res.data.data.flag
								wx.setStorageSync('openid', res.data.data.openid)
								wx.setStorageSync('token', res.data.data.token)
								wx.setStorageSync('showIndex', res.data.data.flag==1?true:false)
								
								that.userInfoRequest()
							}
						},
								
					})
				}
			})
			// uni.login().then((d) => {
			// 	console.log(d)
			// 	that.code = d.code
			// 	wx.request({
			// 		url: getApp().globalData.url + '/sys/reg',
			// 		method: 'POST',
			// 		data: {
			// 			code: d.code
			// 		},
			// 		header: {
			// 			'content-type': 'application/x-www-form-urlencoded',
			// 		},
			// 		success: (res) => {
			// 			if (res.data.code == 20000) {
			// 				that.userinfo = res.data.data
			// 				that.mobile = res.data.data.mobile
			// 				that.flag = res.data.data.flag
			// 				wx.setStorageSync('openid', res.data.data.openid)
			// 				wx.setStorageSync('token', res.data.data.token)
			// 				wx.setStorageSync('showIndex', res.data.data.flag==1?true:false)
							
			// 				that.userInfoRequest()
			// 			}
			// 		},
			
			// 	})
			// });
		},
		
		methods: {
			clickPopup(){
				let that = this
				that.showUserInfo = true
				that.popup = false
				that.showMobile = 2
			},
			close(){
				this.popup = false
			},
			// 跳转首页
			index(){
				let that = this
				
				if(that.flag == 1){
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}else if(that.mobile){
					uni.request({
						url: getApp().globalData.url + '/sys/checkuserbymobile',
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							token: uni.getStorageSync('token') 
						},
						data: {
							mobile: that.mobile
						},
						success(res) {
							if(res.data.flag==1){
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}else{
								that.popup = true
							}
						}
					})
				}else{
					that.showPhone = true
					uni.showToast({
						title: '请先授权手机号',
						icon: 'none'
					})
				}
			},
			
			// 授权
			clickButton(){
				let that = this
				
				that.showUserInfo = true
			},
			
			// 更新成功
			updataSuccess(userInfo){
				let that = this
				// 更新头像
				uni.request({
					url: getApp().globalData.url + '/sys/upheadimg',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					},
					data: {
						headimg: userInfo.headimg
					},
					success(res) {
						if(res.data.code == 20000){
							// 更新昵称
							uni.request({
								url: getApp().globalData.url + '/sys/upnickname',
								header: {
									'content-type': 'application/x-www-form-urlencoded',
									token: uni.getStorageSync('token')
								},
								data: {
									nickname: userInfo.nickname,
								},
								success(res) {
									if(res.data.code == 20000){
										that.userInfoRequest()
									}else{
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										})
									}
								}
							})
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					}
				})
			},
			
			// 更新个人信息
			userInfoRequest(){
				let that = this
				
				uni.request({
					url: getApp().globalData.url + '/sys/getuser',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.code == 20000) {
							that.userInfo = res.data.data
							that.nickname = res.data.data.nickname
							that.mobile = res.data.data.mobile
							console.log(that.nickname)
						}
					}
				})
			},
			
			// 关闭更新
			hidePopup(){
				this.showUserInfo = false
			},
			
			// 获取手机号
			getphonenumber(e){
				let that = this
				
				if(e.detail.code){
					uni.request({
						url: getApp().globalData.url + '/sys/checkuser',
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							token: uni.getStorageSync('token')
						},
						data: {
							code: e.detail.code
						},
						success(res) {
							if(res.data.code==20000){
								if(res.data.flag == 1){
									// uni.reLaunch({
									// 	url: '/pages/index/index'
									// })
									uni.switchTab({
										url: '/pages/index/index'
									})
								}else{
									that.popup = 1
									// uni.reLaunch({
									// 	url: '/pages/index/index?loginNo=1'
									// })
								}
							}else{
								uni.showToast({
									title: res,
									icon: 'none'
								})
							}
							
						}
					})
				}
			},
			
			successPhone(){
				let that = this
				// this.userInfoRequest()
				// uni.reLaunch({
				// 	url: '/pages/index/index'
				// })
				that.popup = 1
				that.mobile = 1
				that.showUserInfo = false
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #03AEA5;
		width: 100%;
		height: 100%;
		overflow: hidden;
	}
	.updatauser{
		top: 500rpx !important;
	}
	.img{
		width: 750rpx;
		height: 100%;
		margin-top: -150rpx;
	}
	.button{
		position: fixed;
		bottom: 50rpx;
		left: 0rpx;
		width: 750rpx;
		height: 600rpx;
		background: linear-gradient(60deg, #009B8F, #00BFAF, #009B8F);
		box-shadow: 2rpx 3rpx 10rpx 0rpx rgba(0,0,0,0.93);
		border-radius: 60rpx;
		border: 2px solid #F4B779;
		// border-image: linear-gradient(0deg, #F8C586, #F4B779) 2 2;
		text-align: center;
		font-family: Alibaba PuHuiTi;
		font-weight: 400;
		font-size: 44rpx;
		// color: rgba(0, 156, 143, 0);
		color: #F4B678;
		line-height: 120rpx;
		overflow: hidden;
		opacity: 0;
	}
	
	
	// 弹窗
	.popup{
		position: fixed;
		top: 0;
		left: 0;
		width: 750rpx;
		height: 100%;
		background: #000000;
		opacity: 0.7;
		overflow: hidden;
		z-index: 998;
	}
	.popup_img{
		position: fixed;
		top: 400rpx;
		left: 87rpx;
		width: 576rpx;
		height: 572rpx;
		overflow: hidden;
		z-index: 999;
	}
	.popup_button{
		position: fixed;
		top: 1000rpx;
		left: 87rpx;
		width: 576rpx;
		height: 97rpx;
		background: #02ADA4;
		border-radius: 18rpx;
		text-align: center;
		font-family: Alibaba PuHuiTi;
		font-weight: 400;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 97rpx;
		overflow: hidden;
		z-index: 999;
	}
	.popup_close{
		position: fixed;
		top: 1200rpx;
		left: 326rpx;
		width: 98rpx;
		height: 98rpx;
		overflow: hidden;
		z-index: 999;
	}
</style>
